package beans;

import models.Article;
import models.Photo;
import models.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xieyicheng on 2014/12/5.
 */
public class ArticleBean {
    public String id;
    public String title;
    public int click;
    public int commentNum;
    public int is_verify;
    public String photoUrl;
    public String authorName;
    public String content;
    public String authorId;
    public long updateTime;

    public static ArticleBean build(Article article) {
            ArticleBean articleBean = new ArticleBean();
            articleBean.id = article.id;
            articleBean.title = article.title;
            articleBean.click = article.click;
            articleBean.is_verify =article.is_verify;
            if(article.content.length() > 250)
            	articleBean.content = article.content.substring(0, 249);
            else
            	articleBean.content = article.content;
            articleBean.commentNum = article.commentNum;
            articleBean.authorId = article.authorId;
            articleBean.updateTime =article.createTime;
            Photo photo = Photo.find("id = ?",article.photoId).first();
            if(photo!=null) {
                articleBean.photoUrl = photo.getPhoto();
            }
            else{
                articleBean.photoUrl = "";
            }
            articleBean.authorName = User.getAuthorNameById(article.authorId);
            return  articleBean;
    }
    public static List<ArticleBean> buildList (List<Article> plist){
        List<ArticleBean> endlist = new ArrayList<ArticleBean>();
        for(int item = 0;item<plist.size();item++){
            ArticleBean bean = ArticleBean.build(plist.get(item));
            endlist.add(bean);

        }
        return endlist;
    }
}
